﻿using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using Common;

namespace ZhaoYan.Manager
{
    public class EnemyManager : ModuleBase, IEnemyManager
    {
        static EnemyManager _instance = null;

        public static EnemyManager Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new EnemyManager ();
                    //GameObject go = GameObject.Instantiate (new GameObject ()) as GameObject;

                    //go.name = "EnemyManager";

                    //_instance = go.AddComponent (typeof (EnemyManager)) as EnemyManager;
                    
                }
                return _instance;
            }
        }

        public List<IUnit> AllMonsters
        {
            get
            {
                return new List<IUnit> (_allEnemies.ToArray ());
            }
        }

        private List<Enemy> _allEnemies = new List<Enemy> ();

        private EnemyManager ()
        {
            AppInterface.EnemyManager = this;
           // InterfaceManager.ServerManager.RegisterMessageHandler (MessageKey.EnemyModel | MessageKey.PlayerModel, OnNetMessage);
        }

        public event Action<HitEventArgs> OnHit;

        void OnNetMessage (object msg, int key)
        {
            HitEventArgs hea = new HitEventArgs ();
            hea.AttakeeID = 0;
            hea.AttakerID = 0;
            hea.DeltaHP = 0;
            hea.MaxHP = 0;
            hea.RemainHP = 0;

            if (OnHit != null)
            {
                OnHit (hea);
            }
        }

        int _monsterCount = 0;
        /// <summary>
        /// 场景正常加载之后，在适当位置加入小怪物
        /// </summary>
        /// <param name="initPosition">初始位置</param>
        /// <param name="unit">单位</param>
        public void AddEnemy (Vector3 initPosition, IEnemy unit)
        {
            Enemy enemy = new Enemy ();

            if (enemy.Initialze (unit, initPosition, _monsterCount ++))
            {
                _allEnemies.Add (enemy);
                enemy.OnDead += new Action<IUnit, IUnit> (enemy_OnDead);
            }
        }

        void enemy_OnDead (IUnit arg1, IUnit arg2)
        {
            _allEnemies.Remove (arg1 as Enemy);
        }

        /// <summary>
        /// 加入场景随机出现怪物
        /// </summary>
        /// <param name="unit"></param>
        public void AddEnemy (IEnemy unit)
        {
            throw new NotImplementedException ();
        }

        IEnemy FindByName (string name)
        {
            return new ZhaoYan.Yezhujing ();
        }

        public void InitRegion (GameObject region)
        {
            Transform[] trans = region.GetComponentsInChildren<Transform> ();

            foreach (Transform tran in trans)
            {
                if (tran.parent != null && tran.parent.name == "Enemies")
                {
                    IEnemy ie = FindByName (tran.name);
                    if (ie != null)
                    {
                        this.AddEnemy (tran.position, ie);
                    }
                    GameObject.Destroy (tran.gameObject);
                }
            }
        }

        /// <summary>
        /// 加入BOSS 
        /// </summary>
        /// <param name="boss"></param>
        public void AddBoss (IBoss boss)
        {
            throw new NotImplementedException ();
        }

        public override void FixedUpdate ()
        {
            foreach (Enemy e in _allEnemies)
            {
                e.FixUpdate ();
            }
        }
    }
}
